package stockblaster.client;

import java.util.HashMap;
import java.util.LinkedList;

import stockblaster.gen.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;

public class CompanyStockInfoActivity extends Activity {
	
	final Activity thisAct = CompanyStockInfoActivity.this;
	TextView companyNameTV, sharePriceTV, percentTV;
	TextView currentFundTV, sharesOwnedTV, sharesValueTV, costTV,
			sharesOwnedTVTop;
	TextView alertTextTV, monthYearTV, companyNameInsideTV, companyInfoTV,
			emptyNewsTV;
	ImageView arrowIV, companyLogoIV;

	Button sharesBut, readNewsBut, companyInfoBut, confirmBut, cancelBut,
			logOutBut, backBut, usernameBut;
	Button objectiveBut, helpBut, allSharesBut, portfolioBut, advanceOneBut;
	Spinner buySellSpinner;
	EditText numberSharesET;

	LinearLayout sharesBridgeLL, readNewsBridgeLL, companyInfoBridgeLL;
	LinearLayout ll;
	ScrollView viewShareBox, viewNewsBox, companyInfoBox, scrollOutBox;

	private InputMethodManager imm;
	boolean isBuy = true; // true if spinner = buy shares, false for sell shares

	Double sharePrice = 0.00;
	int newsSize = 0;

	String companyName = "";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.company_stock_info_view);

		CurrentUser.current_class = CompanyStockInfoActivity.class;
		
		imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		
		
		ll = (LinearLayout) this.findViewById(R.id.newsBox);
		ll.removeAllViews();

		companyNameTV = (TextView) this.findViewById(R.id.companyName);
		sharePriceTV = (TextView) this.findViewById(R.id.sharePrice);
		percentTV = (TextView) this.findViewById(R.id.percent);
		usernameBut = (Button) this.findViewById(R.id.username);
		usernameBut.setText(CurrentUser.getUsername());
		
		
		sharePrice = Double.parseDouble(sharePriceTV.getText().toString()
				.substring(1));
		companyNameInsideTV = (TextView) this
				.findViewById(R.id.companyNameInside);
		companyInfoTV = (TextView) this.findViewById(R.id.companyInfoText);

		currentFundTV = (TextView) this.findViewById(R.id.currentFund);
		currentFundTV.setText(MethodHelper.twoDecimal(CurrentUser.getFund()));
		sharesOwnedTV = (TextView) this.findViewById(R.id.sharesOwned);
		sharesValueTV = (TextView) this.findViewById(R.id.sharesValue);
		costTV = (TextView) this.findViewById(R.id.cost);
		sharesOwnedTVTop = (TextView) this.findViewById(R.id.sharesOwnedTop);
		emptyNewsTV = (TextView) this.findViewById(R.id.emptyNewsText);
		emptyNewsTV.setVisibility(View.GONE);

		alertTextTV = (TextView) this.findViewById(R.id.alertText);

		monthYearTV = (TextView) this.findViewById(R.id.monthYear);
		monthYearTV.setText("" + CurrentUser.getMonth() + "/"
				+ CurrentUser.getYear());

		arrowIV = (ImageView) this.findViewById(R.id.arrow);
		companyLogoIV = (ImageView) this.findViewById(R.id.companyLogo);

		sharesBut = (Button) this.findViewById(R.id.button1);
		readNewsBut = (Button) this.findViewById(R.id.button2);
		confirmBut = (Button) this.findViewById(R.id.button3);
		confirmBut.setEnabled(false);
		cancelBut = (Button) this.findViewById(R.id.button4);
		logOutBut = (Button) this.findViewById(R.id.button5);
		backBut = (Button) this.findViewById(R.id.button6);
		objectiveBut = (Button) this.findViewById(R.id.objectiveButton);
		helpBut = (Button) this.findViewById(R.id.helpButton);
		companyInfoBut = (Button) this.findViewById(R.id.button7);
		portfolioBut = (Button) this.findViewById(R.id.button8);
		advanceOneBut = (Button) this.findViewById(R.id.advance1month);

		numberSharesET = (EditText) this.findViewById(R.id.editText1);
		numberSharesET.setRawInputType(Configuration.KEYBOARD_12KEY);
		

		sharesBridgeLL = (LinearLayout) this.findViewById(R.id.sharesBridge);
		readNewsBridgeLL = (LinearLayout) this
				.findViewById(R.id.readNewsBridge);
		companyInfoBridgeLL = (LinearLayout) this
				.findViewById(R.id.companyInfoBridge);

		viewShareBox = (ScrollView) this.findViewById(R.id.viewShareBox);
		viewNewsBox = (ScrollView) this.findViewById(R.id.viewNewsBox);
		companyInfoBox = (ScrollView) this.findViewById(R.id.companyInfoBox);
		scrollOutBox = (ScrollView) this.findViewById(R.id.scrollOutBox);
		
//		RelativeLayoutThatDetectsSoftKeyboard mainLayout = (RelativeLayoutThatDetectsSoftKeyboard)
//				findViewById(R.id.mainLayout);
//        mainLayout.setListener(this);
        
//		numberSharesET.requestFocus();
//		numberSharesET.setOnTouchListener(new View.OnTouchListener() {
//			public boolean onTouch(View v, MotionEvent event) {
//				scrollOutBox.scrollTo(0, scrollOutBox.getHeight() / 2);
//				return false;
//			}
//		});

		// need to get the name parameter from user's input
		// i.e. use Bundle
		// Bundle bundle = getIntent().getExtras();
		companyName = CurrentUser.getCompanyPick();

		initCompanyInfo(companyName);
		allSharesBut = (Button) this.findViewById(R.id.allButton);
		
		setNews();
		setVisibility();
		initSpinners();
		setOnClickListeners();
		setOnKeyListeners();
		
		if (CurrentUser.getUsername().equals("Januardo Jankowski")) {
			dialogPopUps(false);
			objectiveBut.setText("Objectives");
		}
		if (CurrentUser.doesCompanyExist(companyName))
			initUserData();

		setAllSharesButton(isBuy);
		setEnability();
		// remove the $ sign
		sharePrice = Double.parseDouble(sharePriceTV.getText().toString()
				.substring(1));
	}
	
//	public void onSoftKeyboardShown(boolean isShowing) {
//		scrollOutBox.scrollTo(0, scrollOutBox.getHeight() / 2);
//    }

	@Override
	public void onBackPressed() {
		//setBuySellSelected();
		CurrentUser.currentCompanyStockInfoTab = 1;
		//emptyNewsTV.setVisibility(View.GONE);
		MethodHelper.switch_activity(CompanyStockInfoActivity.this,
				MainPageActivity.class, false);
	}

	public void onResume() {
		super.onResume();
		ll.removeAllViews();
		setNews();
		setEnability();
		setBuySellSelected();
		initCompanyInfo(companyName);
		initUserData();
		monthYearTV.setText("" + CurrentUser.getMonth() + "/"
				+ CurrentUser.getYear());
		currentFundTV.setText(MethodHelper.twoDecimal(CurrentUser.getFund()));
//		if (!CurrentUser.getUsername().equals("Januardo Jankowski")) {
//			CurrentUser.checkObjective(thisAct);
//		}
	}

	private void setEnability() {
		if (CurrentUser.getUsername().equals("Januardo Jankowski")) {
			if (CurrentUser.getMonth() > 1) {
				advanceOneBut.setEnabled(true);
				monthAdvancedPopUps();
			} else if ((CurrentUser.getHelpNum() < 5)) {
				advanceOneBut.setEnabled(false);
			}
		}
	}

	private void monthAdvancedPopUps() {
		if (CurrentUser.getUsername().equals("Januardo Jankowski")) {
			int num = CurrentUser.getHelpNum();
			switch (num) {
			case 5:
				MethodHelper.show_dialogs(CompanyStockInfoActivity.this,
						MainPageActivity.class, num);
				CurrentUser.addHelpNum();
				break;
			}
		}
	}

	private void setAllSharesButton(boolean _isBuy) {
		if (_isBuy) {
			allSharesBut.setText("Buy All");
		} else {
			allSharesBut.setText("Sell All");
		}
	}

	private void initUserData() {
		int shareNum = CurrentUser.getShare(companyName);
		sharesOwnedTV.setText("" + shareNum);
		sharesOwnedTVTop.setText("" + shareNum);
		Double sharesValue = shareNum
				* Companies.getCompanyShareValue(companyName);
		sharesValueTV.setText(MethodHelper.twoDecimal(sharesValue));
		if (shareNum > 0) {
			buySellSpinner.setEnabled(true);
		}

	}

	private void dialogPopUps(boolean isInPage) {
		if (CurrentUser.getUsername().equals("Januardo Jankowski")) {
			int num = CurrentUser.getHelpNum();
			if (isInPage) {
				switch (num) {
				case 2:
					MethodHelper.show_dialogs(CompanyStockInfoActivity.this,
							MainPageActivity.class, num);
					CurrentUser.addHelpNum();
					break;
				}
			} else {
				switch (num) {
				case 1:
					MethodHelper.show_dialogs(CompanyStockInfoActivity.this,
							MainPageActivity.class, num);
					CurrentUser.addHelpNum();
					break;
				}
			}
		}
	}

	private void firstFourDialog() {
		if (CurrentUser.getUsername().equals("Januardo Jankowski")) {
			int num = CurrentUser.getHelpNum();
			if (!CurrentUser.firstFourTriggered) {
				switch (num) {
				case 4:
					MethodHelper.show_dialogs(CompanyStockInfoActivity.this,
							MainPageActivity.class, num);
					CurrentUser.addHelpNum();
					break;
				}
			}
		}
	}

	private void setBuySellSelected() {
		if (!sharesBut.isSelected()) {
			emptyNewsTV.setVisibility(View.GONE);
			sharesBut.setSelected(true);
			sharesBridgeLL.setVisibility(View.VISIBLE);
			viewShareBox.setVisibility(View.VISIBLE);

			readNewsBut.setSelected(false);
			readNewsBridgeLL.setVisibility(View.INVISIBLE);
			viewNewsBox.setVisibility(View.INVISIBLE);
			companyInfoBut.setSelected(false);
			companyInfoBridgeLL.setVisibility(View.INVISIBLE);
			companyInfoBox.setVisibility(View.INVISIBLE);
			CurrentUser.currentCompanyStockInfoTab = 1;
		}
	}

	private void setReadNewsSelected() {
		if (!readNewsBut.isSelected()) {
			if (newsSize == 0)
				emptyNewsTV.setVisibility(View.VISIBLE);
			readNewsBut.setSelected(true);
			readNewsBridgeLL.setVisibility(View.VISIBLE);
			viewNewsBox.setVisibility(View.VISIBLE);

			sharesBut.setSelected(false);
			sharesBridgeLL.setVisibility(View.INVISIBLE);
			viewShareBox.setVisibility(View.INVISIBLE);
			companyInfoBut.setSelected(false);
			companyInfoBridgeLL.setVisibility(View.INVISIBLE);
			companyInfoBox.setVisibility(View.INVISIBLE);
			CurrentUser.currentCompanyStockInfoTab = 2;
		}
	}

	private void setCompanyInfoSelected() {
		if (!companyInfoBut.isSelected()) {
			emptyNewsTV.setVisibility(View.GONE);
			companyInfoBut.setSelected(true);
			companyInfoBridgeLL.setVisibility(View.VISIBLE);
			companyInfoBox.setVisibility(View.VISIBLE);

			sharesBut.setSelected(false);
			sharesBridgeLL.setVisibility(View.INVISIBLE);
			viewShareBox.setVisibility(View.INVISIBLE);
			readNewsBut.setSelected(false);
			readNewsBridgeLL.setVisibility(View.INVISIBLE);
			viewNewsBox.setVisibility(View.INVISIBLE);
			CurrentUser.currentCompanyStockInfoTab = 3;
		}
	}

	private void setNews() {
		HashMap<String, LinkedList<String[]>> news = Companies.getCompanyNews();
		if (news == null) {
			return;
		}
		LinkedList<String[]> allNews = news.get(companyName);
		if (allNews == null) {
			newsSize = 0;
			return;
		}
		int max = allNews.size();
		int j = 1;
		RelativeLayout layout;
		LayoutParams lp;

		newsSize = max;
		
		int actualNewsNum = 0;
		for (int i = 0; i < max; ++i) {
			// String cName = namesTemp.get(i);
			// set up the layout so we can position them nicely
			layout = new RelativeLayout(this);
			layout.setId(j);
			layout.setClickable(true);
			final String hl = allNews.get(i)[0];
			if (hl.equals(""))
				continue;
			else
				++actualNewsNum;
			
			// text view for headline
			final TextView tv = new TextView(this);
			tv.setId(j + 1);
			tv.setText(hl);
			tv.setTextColor(Color.CYAN);
			tv.setTextSize(25);
			layout.setPadding(5, 5, 0, 0);
			lp = new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
					RelativeLayout.LayoutParams.WRAP_CONTENT);
			layout.addView(tv, lp);

			// text view for content
			final TextView tv1 = new TextView(this);
			tv1.setId(j + 2);
			tv1.setText(allNews.get(i)[1].toString());
			tv1.setTextColor(Color.WHITE);
			tv1.setTextSize(18);
			layout.setPadding(0, 5, 0, 0);
			lp = new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
					RelativeLayout.LayoutParams.WRAP_CONTENT);
			lp.addRule(RelativeLayout.BELOW, tv.getId());
			layout.addView(tv1, lp);

			// this is the divider line
			final View v = new View(this);
			v.setBackgroundColor(Color.WHITE);
			v.setLayoutParams(new LayoutParams(
					LinearLayout.LayoutParams.FILL_PARENT, 1));

			ll.addView(layout);
			ll.addView(v);
		}
		
		if (actualNewsNum == 0) {
			newsSize = 0;
		} else
			emptyNewsTV.setVisibility(View.GONE);
	}

	private void setVisibility() {
		usernameBut.setVisibility(View.INVISIBLE);
		alertTextTV.setVisibility(View.INVISIBLE);
		int num = CurrentUser.currentCompanyStockInfoTab;
		switch (num) {
		case 1:
			setBuySellSelected();
			break;
		case 2:
			setReadNewsSelected();
			break;
		case 3:
			setCompanyInfoSelected();
			break;
		}
	}

	/**
	 * Method to initialize the spinners for countries and currencies
	 */
	private void initSpinners() {
		buySellSpinner = (Spinner) this.findViewById(R.id.spinner1);
		ArrayAdapter<CharSequence> countryAdapter = ArrayAdapter
				.createFromResource(this, R.array.option_shares,
						android.R.layout.simple_spinner_item);
		countryAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		buySellSpinner.setAdapter(countryAdapter);
		buySellSpinner.setOnItemSelectedListener(new buySellSharesListener());
		buySellSpinner.setEnabled(false);
	}

	/**
	 * Method to initialize all the company info
	 */
	private void initCompanyInfo(String cName) {
		companyLogoIV.setBackgroundResource(Companies.getCompanyLogo(cName));
		companyNameTV.setText(cName);
		companyNameInsideTV.setText(cName);
		int arrowImage = Companies.getArrow(cName);
		if (arrowImage == R.drawable.stock_up) {
			percentTV.setTextColor(getResources().getColor(R.color.lime_green));
		} else if (arrowImage == R.drawable.stock_down) {
			percentTV.setTextColor(getResources().getColor(R.color.red));
		}
		percentTV.setText(Companies.getCompanyPercentGrowth(cName) + "%");
		arrowIV.setBackgroundResource(arrowImage);
		companyInfoTV.setText(Companies.getInfo(cName));
		sharePrice = Companies.getCompanyShareValue(cName);
		sharePriceTV.setText(MethodHelper.twoDecimal(sharePrice));
		
	}

	/**
	 * Method to initialize all the onClickListener
	 */
	private void setOnClickListeners() {
		sharesBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				setBuySellSelected();
			}
		});

		readNewsBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				setReadNewsSelected();
			}
		});

		companyInfoBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				setCompanyInfoSelected();
			}
		});

		portfolioBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				CurrentUser.currentMainTab = 2;
				MethodHelper.switch_activity(CompanyStockInfoActivity.this,
						MainPageActivity.class, false);
			}
		});

		advanceOneBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Companies.resetNewsData();
				CurrentUser.addMonth();
				UserRounds.updateCompanies();
				// MethodHelper.switch_activity(MainPageActivity.this,
				// MainPageActivity.class, false);
				CompanyStockInfoActivity.this.onResume();
			}
		});

		confirmBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				alertTextTV.setVisibility(View.INVISIBLE);
				imm.hideSoftInputFromWindow(numberSharesET.getWindowToken(), 0);
				numberSharesET.clearFocus();
				Integer sharesNum = Integer.parseInt(numberSharesET.getText()
						.toString());
				processBuySellAction(sharesNum);
			}
		});

		cancelBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				alertTextTV.setVisibility(View.INVISIBLE);
				imm.hideSoftInputFromWindow(numberSharesET.getWindowToken(), 0);
				numberSharesET.clearFocus();
				numberSharesET.setText("");
				costTV.setText("$0.00");
				confirmBut.setEnabled(false);
			}
		});

		logOutBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				MethodHelper.show_dialogs(CompanyStockInfoActivity.this,
						SplashActivity.class, MethodHelper.LOG_OUT);
			}
		});

		objectiveBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				CurrentUser.setFromClass(CompanyStockInfoActivity.class,
						CurrentUser.OBJECTIVE);
				MethodHelper.switch_activity(CompanyStockInfoActivity.this,
						ObjectiveActivity.class, false);
			}
		});

		helpBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				CurrentUser.setFromClass(CompanyStockInfoActivity.class,
						CurrentUser.HELP);
				MethodHelper
						.switch_activity(thisAct, HelpActivity.class, false);
			}
		});

		backBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				onBackPressed();
			}
		});

		allSharesBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (isBuy) {
					Double numShares = CurrentUser.getFund() / sharePrice;
					numShares = Math.floor(numShares);
					numberSharesET.setText(""+numShares.intValue());
					if (numShares != 0) {
						costTV.setText(MethodHelper.twoDecimal(numShares
								* Companies.getCompanyShareValue(companyName)));
						//processBuySellAction(numShares.intValue());
					}
				} else {
					int numShares = CurrentUser.getShare(companyName);
					numberSharesET.setText(""+numShares);
					if (numShares != 0) {
						costTV.setText(MethodHelper.twoDecimal(numShares
								* Companies.getCompanyShareValue(companyName)));
						//processBuySellAction(numShares);
					}
				}
				imm.hideSoftInputFromWindow(
						numberSharesET.getWindowToken(), 0);
				numberSharesET.clearFocus();
				checkResources();
			}
		});
		
		usernameBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				MethodHelper.showUserProfileDialog(thisAct, getResources());
			}
		});
	}

	private void processBuySellAction(int sharesNum) {
		Double currentFund = CurrentUser.getFund();
		// Double.parseDouble(currentFundTV.getText().toString()
		// .substring(1)); //remove the $ sign

		Integer sharesOwned = Integer.parseInt(sharesOwnedTV.getText()
				.toString());

		if (isBuy) {
			currentFund -= Double.parseDouble(costTV.getText().toString()
					.substring(1));
			sharesOwned += sharesNum;
			if (currentFund < 0) {
				showAlertText(View.VISIBLE, 1);
				confirmBut.setEnabled(false);
				return;
			}
		} else if (!isBuy) {
			currentFund += Double.parseDouble(costTV.getText().toString()
					.substring(1));
			sharesOwned -= sharesNum;
			if (sharesOwned < 0) {
				showAlertText(View.VISIBLE, 2);
				confirmBut.setEnabled(false);
				return;
			}
		}
		numberSharesET.setText("");

		// deal with database
		if (sharesOwned == 0) {
			CurrentUser.removeCompanyShare(companyName);
		} else {
			CurrentUser.addShare(companyName, sharesOwned);
		}
		CurrentUser.setFund(currentFund);

		Double sharesValue = sharesOwned * sharePrice;

		CurrentUser.updateGainLoss(CurrentUser.getMonth(), companyName,
				sharesValue);

		sharesValueTV.setText(MethodHelper.twoDecimal(sharesValue));
		currentFundTV.setText(MethodHelper.twoDecimal(currentFund));
		costTV.setText("$0.00");

		sharesOwnedTV.setText("" + sharesOwned);
		sharesOwnedTVTop.setText("" + sharesOwned);
		confirmBut.setEnabled(false);

		if (Integer.parseInt(sharesOwnedTV.getText().toString()) > 0) {
			buySellSpinner.setEnabled(true);
		} else {
			buySellSpinner.setSelection(0); // set to Buy Shares
			buySellSpinner.setEnabled(false);
		}

		if (CurrentUser.getUsername().equals("Januardo Jankowski")) {
			dialogPopUps(true);

			if (CurrentUser.getCompanyNames().size() == 4) {
				firstFourDialog();
				advanceOneBut.setEnabled(true);
				CurrentUser.setFirstFourTriggered(true);
			}
		} //else {
//			CurrentUser.checkObjective(thisAct);
//			currentFundTV.setText(MethodHelper.twoDecimal(CurrentUser.getFund()));
//		}
	}

	/**
	 * Method to setOnKeyListener
	 */
	private void setOnKeyListeners() {
		numberSharesET.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_ENTER) {
					imm.hideSoftInputFromWindow(
							numberSharesET.getWindowToken(), 0);
					numberSharesET.clearFocus();
					return true;
				} else {
					checkResources();
					return false;
				}
			}
		});
	}

	private void checkResources() {
		String sharesText = numberSharesET.getText().toString();
		if (sharesText.equals("")) {
			costTV.setText(MethodHelper.twoDecimal(0.00));
			confirmBut.setEnabled(false);
		} else {
			Double shares = Double.parseDouble(sharesText);
			Double cost = shares * sharePrice;
			costTV.setText(MethodHelper.twoDecimal(cost));
			if (isBuy) {
				if (Double.parseDouble(currentFundTV.getText().toString()
						.substring(1)) < cost) {
					showAlertText(View.VISIBLE, 1);
					confirmBut.setEnabled(false);
				} else {
					alertTextTV.setVisibility(View.INVISIBLE);
					confirmBut.setEnabled(true);
				}
			} else {
				if (shares > Double.parseDouble(sharesOwnedTV.getText()
						.toString())) {
					showAlertText(View.VISIBLE, 2);
					confirmBut.setEnabled(false);
				} else {
					alertTextTV.setVisibility(View.INVISIBLE);
					confirmBut.setEnabled(true);
				}
			}
		}
	}

	/**
	 * Method for showing alert text
	 * 
	 * @param visibility
	 *            View.INVISIBLE or View.GONE
	 * @param id
	 *            1 for notEnoughFund; 2 for notEnoughShares
	 */
	private void showAlertText(int visibility, int id) {
		if (id == 1) {
			alertTextTV.setVisibility(visibility);
			alertTextTV.setText(getResources()
					.getString(R.string.notEnoughFund));
		} else if (id == 2) {
			alertTextTV.setVisibility(visibility);
			alertTextTV.setText(getResources().getString(
					R.string.notEnoughShares));
		}
	}

	/**
	 * Listener class for buySellSpinner
	 * 
	 * @author Theodore
	 * 
	 */
	public class buySellSharesListener implements OnItemSelectedListener {
		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {

			switch (new Integer((int) id)) {
			case 0:
				isBuy = true;
				costTV.setTextColor(Color.RED);
				allSharesBut.setText("Buy Max");
				break;
			case 1:
				isBuy = false;
				costTV.setTextColor(Color.GREEN);
				allSharesBut.setText("Sell Max");
				break;
			}
		}

		public void onNothingSelected(AdapterView<?> parent) {
			// Do nothing.
		}

		public void onClick() {

		}
	}
}
